package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.mapper.ReplyMapper;
import com.example.blog.pojo.Reply;
import com.example.blog.pojo.Result;
import com.example.blog.service.ReplyService;
import com.example.blog.util.TokenUtil.decryptToken;
import com.example.blog.util.TokenUtil.spiltUtils;
import com.example.blog.util.UserUtils.UserUtils;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ReplyServiceImpl extends ServiceImpl<ReplyMapper, Reply> implements ReplyService {
    @Resource
    private ReplyMapper replyMapper;
    @Resource
    private UserUtils userIdUtil;

    @Override
    public Result<T> saveReplyComment(Reply reply, String text, int commentId, String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        Integer userId = userIdUtil.getUserId(userName);
        String headerUrl = userIdUtil.getHeaderUrl(userName);
        reply.setCommentId((long) commentId);
        reply.setUserId(Long.valueOf(userId));
        reply.setHeaderurl(headerUrl);
        reply.setContent(SensitiveWordHelper.replace(text));
        reply.setCreateTime(new Date());
        replyMapper.insert(reply);
        return Result.success("回复成功！");
    }

    @Override
    public Result<List<Reply>> getReplyComment(int commentId) {
        QueryWrapper<Reply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", commentId);
        List<Reply> replies = replyMapper.selectList(queryWrapper);
        return Result.success(replies);

    }
}
